%% Table 1: Summary statistics
clc;
clear;

%% Loading data

load('Data\Special_Transactions_Pseudo.mat');
Specials.VolRate = Specials.Rate.*Specials.Volume; 
load('Data\GC_Transactions_Pseudo.mat');
GC.VolRate = GC.Rate.*GC.Volume;
GC.RefMonth = dateshift(GC.refDate,'start','month');

% download data on the DFR (see: https://www.ecb.europa.eu/stats/policy_and_exchange_rates/key_ecb_interest_rates/html/index.en.html) and replace code below:
DFR = table();%this creates pseudo-data
t1 = datetime(2010,1,1);%this creates pseudo-data
t2 = datetime(2019,1,1);%this creates pseudo-data
DFR.refDate = [t1:t2]';%this creates pseudo-data
DFR.DFR = 0.5.*ones(1,length(DFR.refDate))';%this creates pseudo-data

GC = outerjoin(GC,DFR,'Type','left','MergeKeys',1,'LeftKey','refDate','RightKey','refDate');  

clear t1 t2

%% Summary statistics for access vs nonaccess banks (upper part of Table 1)

% Trade size (mn)
SumStatA = varfun(@nanmean, GC, 'InputVariables', {'Volume'}, 'GroupingVariables', {'ECBAccessLender'  });
SumStatA = removevars(SumStatA, 'GroupCount');
SumStatA.nanmean_VolumeMn = round(SumStatA.nanmean_Volume./1000000,1);
SumStatA = removevars(SumStatA, 'nanmean_Volume');
Table1_UpperPanel = unstack(SumStatA,'nanmean_VolumeMn','ECBAccessLender','NewDataVariableNames',{'NoAccess','Access'});
Table1_UpperPanel.RowLabel(1) = string('Trade size (mn)');

clear SumStatA

% Average repo rates for access vs nonaccess banks (GC >/< DFR) 

% Step 1: Calculating daily, average GC rate
Result=varfun(@nansum, GC, 'InputVariables', {'VolRate','Volume'}, 'GroupingVariables', {'refDate'});
Result = removevars(Result,'GroupCount');
Result.VolWeight=Result.nansum_VolRate./Result.nansum_Volume;
Result = removevars(Result, {'nansum_VolRate','nansum_Volume'});
Result.Properties.VariableNames{'VolWeight'} = 'VolWeightAvgRate';
GC = outerjoin(GC,Result,'MergeKeys',1,'LeftKey','refDate','RightKey','refDate');

clear Result

% Step 2: Determine if GC >/< DFR
GC.AvgRateBelow = GC.VolWeightAvgRate < GC.DFR;

% Step 3: Calculate averages
Result = varfun(@nansum, GC, 'InputVariables', {'VolRate','Volume'}, 'GroupingVariables', {'ECBAccessLender' 'AvgRateBelow'});
Result = removevars(Result,'GroupCount');
Result.VolWeight = round(Result.nansum_VolRate./Result.nansum_Volume,2);
Result = removevars(Result, {'nansum_VolRate','nansum_Volume'});
Result = unstack(Result,'VolWeight','ECBAccessLender','NewDataVariableNames',{'NoAccess','Access'});

% Step 4: Adding infos to table
Table1_UpperPanel(2,:)={Result.NoAccess(Result.AvgRateBelow==0),Result.Access(Result.AvgRateBelow==0),string('Repo rate (GC < DFR)')};
Table1_UpperPanel(3,:)={Result.NoAccess(Result.AvgRateBelow==1),Result.Access(Result.AvgRateBelow==1),string('Repo rate (GC > DFR)')};

clear Result

% Interquartile range
SumStatA = varfun(@My25Prct, GC, 'InputVariables', {'Rate'}, 'GroupingVariables', {'ECBAccessLender'});
SumStatA = removevars(SumStatA,'GroupCount');
SumStatB = varfun(@My75Prct, GC, 'InputVariables', {'Rate'}, 'GroupingVariables', {'ECBAccessLender'});
SumStatB = removevars(SumStatB,'GroupCount');
Result = outerjoin(SumStatA,SumStatB,'MergeKeys',1,'LeftKey','ECBAccessLender','RightKey','ECBAccessLender');
Result.InterQuartRange = round(Result.My75Prct_Rate-Result.My25Prct_Rate,2);
Result = removevars(Result, {'My25Prct_Rate','My75Prct_Rate'});
Result = unstack(Result,'InterQuartRange','ECBAccessLender','NewDataVariableNames',{'NoAccess','Access'});
Table1_UpperPanel(4,:)={Result.NoAccess,Result.Access,string('Interquartile range')};

clear SumStatA SumStatB Result

% Remaining summary statistics can be calculated accordingly using confidential bank-level information.

%% Summary statistics for eligible vs noneligible assets (lower part of Table 1)

% Trade size (mn)
SumStatA = varfun(@nanmean, Specials, 'InputVariables', {'Volume'}, 'GroupingVariables', {'HypEligible'});
SumStatA = removevars(SumStatA, 'GroupCount');
SumStatA.nanmean_VolumeMn = round(SumStatA.nanmean_Volume./1000000,1);
SumStatA = removevars(SumStatA, 'nanmean_Volume');
Table1_LowerPanel = unstack(SumStatA,'nanmean_VolumeMn','HypEligible','NewDataVariableNames',{'NonEligible','Eligible'});
Table1_LowerPanel.RowLabel(1) = string('Trade size (mn)');

clear SumStatA

% Average repo rates for eligible vs noneligible assets (pre-/post-QE) 

% Step 1: Determine QE-period
Specials.QE = Specials.refDate>datetime(2015,3,8); %start of QE, see: https://www.ecb.europa.eu/mopo/implement/app/html/index.en.html#pspp

% Step 2: Calculate averages
Result = varfun(@nansum, Specials, 'InputVariables', {'VolRate','Volume'}, 'GroupingVariables', {'HypEligible' 'QE'});
Result.VolWeight = round(Result.nansum_VolRate./Result.nansum_Volume,2);
Result = removevars(Result, {'nansum_VolRate','nansum_Volume'});
Result = unstack(Result,'VolWeight','HypEligible','NewDataVariableNames',{'NonEligible','Eligible'});

% Step 3: Adding infos to table
Table1_LowerPanel(2,:)={Result.NonEligible(Result.QE==0),Result.Eligible(Result.QE==0),string('Repo rate (pre-QE)')};
Table1_LowerPanel(3,:)={Result.NonEligible(Result.QE==1),Result.Eligible(Result.QE==1),string('Repo rate (post-QE)')};

clear Result

%Interquartile range
SumStatA = varfun(@My25Prct, Specials, 'InputVariables', {'Rate'}, 'GroupingVariables', {'HypEligible'});
SumStatA = removevars(SumStatA,'GroupCount');
SumStatB = varfun(@My75Prct, Specials, 'InputVariables', {'Rate'}, 'GroupingVariables', {'HypEligible'});
SumStatB = removevars(SumStatB,'GroupCount');
Result = outerjoin(SumStatA,SumStatB,'MergeKeys',1,'LeftKey','HypEligible','RightKey','HypEligible');
Result.InterQuartRange = round(Result.My75Prct_Rate-Result.My25Prct_Rate,2);
Result = removevars(Result, {'My25Prct_Rate','My75Prct_Rate'});
Result = unstack(Result,'InterQuartRange','HypEligible','NewDataVariableNames',{'NonEligible','Eligible'});

Table1_LowerPanel(4,:)={Result.NonEligible,Result.Eligible,string('Interquartile range')};
clear SumStatA SumStatB Result

% Remaining summary statistics can be calculated accordingly using bond-level information for issue size, tenor, and coupon rate.
